var xmlhttp;
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
} 

function MakeCurrentPage(i)
{
	var uls = document.getElementById("menu").getElementsByTagName("ul");
	var lis = uls[0].getElementsByTagName("li");
	lis[i - 1].getElementsByTagName("a")[0].className = "current";
}
function SortBook(field, category, kind)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	var order = document.getElementById('selectOrder').value;
	var sort = document.getElementById('selectSort').value;
	var url="";
	if(kind == 0)	
		url = "../common/getbookbycategory.php";
	else
		url = "../user/getbookbycategory.php";
	url=url + "?field=" + field + "&category=" + category +"&orderby=" + order + "&sort=" + sort + "&currentpage=1";
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=sortBookStateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function sortBookStateChanged()
{
	if (xmlhttp.readyState==4 && xmlhttp.status == 200)
	{
		document.getElementById('divBook').innerHTML="<br/>"+xmlhttp.responseText; 
	}
}

function download(userId, ebookId, file)
{
	document.location.href = "../user/kt_download.php?f=" + file + "&u=" + userId + "&e=" + ebookId;
}

function admin_download(userId, ebookId, file)
{
	document.location.href = "../admin/download.php?f=" + file + "&u=" + userId + "&e=" + ebookId;
}

function addToNeededList(userId, ebookId)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	var url="addToNeededList.php?u=" + userId + "&e=" + ebookId;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=addToNeededListStateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function addToNeededListStateChanged()
{
	if (xmlhttp.readyState==4 && xmlhttp.status == 200)
	{		
		alert("Đã thêm vào danh sách cần download");
		document.getElementById('divNeededList').innerHTML=xmlhttp.responseText;		
	}
}

function deleteNeededList(userId, ebookId)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	var url="xl_neededlist.php?u=" + userId + "&e=" + ebookId;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=deleteNeededListStateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function deleteNeededListStateChanged()
{
	if (xmlhttp.readyState==4 && xmlhttp.status == 200)
	{
		document.getElementById('divNeededList').innerHTML=xmlhttp.responseText;		
	}
}